package com.libui.webview.jsinterface.handlers

import com.libbase.cast
import com.libbase.utils.ThreadUtils
import com.libui.navigatonbar.MenuData
import com.libui.webview.WebViewEx
import com.libui.webview.jsinterface.WebViewHandlerInterface

class SetRightMenusHandler : WebViewHandlerInterface {
    companion object {
        const val id: String = "setRightMenus"
    }

    override val id: String
        get() {
            return RequestHandler.id
        }

    override fun handle(webView: WebViewEx?, params: Map<String, Any>?) {
        if (webView == null) {
            return
        }

        if (params == null) {
            return
        }

        val uuid: String = params["uuid"] as? String ?: return

        val menuDatas = cast<List<Map<String, Any>>>(params["data"])?.let { data ->
            data.map { MenuData.create(it, webView) }
        }

        ThreadUtils.instance.executeInMainThread {
            webView.delegate?.setRightMenus(webView, menuDatas)
        }

        runSuccessCallback(webView, uuid, 0, "request:ok", null, params)
    }
}